﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: XmlFile.cs
// 作 者：HeXingXiang
// 日 期：2011/03/06
// 描 述：Xml文件控制类
// 版 本：2.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 2.00   2011/03/06   HeXingXiang       无
//==================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Xml;
using System.IO;
using ZX.Common.Enum;

namespace ZX.Common.File
{
    /// <summary>
    /// Xml文件控制类
    /// </summary>
    [Serializable]
    public class XmlFile
    {
        private string m_FilePath = String.Empty;
        /// <summary>
        /// XML文档路径 
        /// </summary>
        public string XmlFilePath
        {
            get { return m_FilePath; }
            set { m_FilePath = CFile.GetPhyPath(value); }
        }

        /// <summary>
        /// 构造函数,不做任何操作
        /// </summary>
        public XmlFile()
        { }
        /// <summary>
        /// 构造函数,获取XML文件主节点
        /// </summary>
        /// <param name="filePath">XML文件路径</param>
        public XmlFile(string filePath)
        {
            m_FilePath = filePath;
        }

        #region ======= Function region ======
        /*
         * XPath : /根节点/子节点
         */
        /// <summary>
        /// 追加节点(在总节点下指定子节点进行追加)
        /// </summary>
        /// <param name="XPathString">查询指定子节点(欲追加节点的父节点)表达式</param>
        /// <param name="NodeName">欲追加节点名称</param>
        /// <param name="AttributeValues">欲追加节点带属性或节点值</param>
        /// <returns>true-成功,false-失败</returns>
        public bool AppendChild(string XPathString, string NodeName, params string[] AttributeValues)
        {
            XmlNode _XmlNode = FindXmlNode(XPathString);
            if (_XmlNode != null)
            {
                XmlNode _NewNode = CreateXmlNode(NodeName, AttributeValues);
                if (_NewNode != null)
                {
                    _XmlNode.AppendChild(_NewNode);
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }

        /// <summary>
        /// 追加节点(追加根节点的子节点)
        /// </summary>
        /// <param name="NodeName">欲追加节点名称</param>
        /// <param name="AttributeValues">欲追加节点带属性或节点值</param>
        /// <returns>true-成功,false-失败</returns>
        public bool AppendChild(string NodeName, params string[] AttributeValues)
        {
            XmlNode _XmlNode = LoadXml();
            if (_XmlNode != null)
            {
                XmlNode _NewNode = CreateXmlNode(NodeName, AttributeValues);
                if (_NewNode != null)
                {
                    _XmlNode.AppendChild(_NewNode);
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }

        /// <summary>
        /// 追加节点(在总节点下指定子节点进行追加)
        /// </summary>
        /// <param name="ParentNode">欲追加节点的父节点</param>
        /// <param name="NodeName">欲追加节点名称</param>
        /// <param name="AttributeValues">欲追加节点带属性或节点值</param>
        /// <returns>非null-成功,null-失败</returns>
        public XmlNode AppendChild(XmlNode ParentNode, string NodeName, params string[] AttributeValues)
        {
            if (ParentNode == null)
                return null;
            else
            {
                XmlNode _NewNode = CreateXmlNode(NodeName, AttributeValues);
                if (_NewNode != null)
                    return ParentNode.AppendChild(_NewNode);
                else
                    return null;
            }
        }

        /// <summary>
        /// 追加节点(在总节点下指定子节点进行追加)
        /// </summary>
        /// <param name="ParentNode">欲追加节点的父节点</param>
        /// <param name="NodeValues">欲追加节点名称或节点文本</param>
        /// <returns>非null-成功,null-失败</returns>
        public void AppendChild(XmlNode ParentNode, params string[] NodeValues)
        {
            if (ParentNode == null || NodeValues == null)
                return;
            int s4Count;
            if (NodeValues.Length % 2 == 0)
                s4Count = NodeValues.Length;
            else
                s4Count = NodeValues.Length - 1;
            for (int Int_i = 0; Int_i < s4Count; Int_i += 2)
            {
                XmlNode new_Node = AppendChild(ParentNode, NodeValues[Int_i]);
                new_Node.InnerText = NodeValues[Int_i + 1];
            }
        }

        /// <summary>
        /// 获取总节点XML字符串
        /// </summary>
        /// <returns>XML字符串</returns>
        public string ToXmlString()
        {
            XmlNode _XmlNode = LoadXml();
            if (_XmlNode != null)
                return _XmlNode.OuterXml;
            else
                return string.Empty;
        }

        /// <summary>
        /// 保存XML文档,覆盖打开的文档
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <returns>true-保存成功,false-失败</returns>
        public bool SaveXml(string XPathString)
        {
            if (!CFile.FilePathIsValid(m_FilePath, ".xml"))
                return false;
            XmlNode _XmlNode = FindXmlNode(XPathString);
            if (_XmlNode != null)
            {
                SaveXml(_XmlNode, m_FilePath);
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 保存XML文档,另存为新的文档
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <param name="FilePath">文档存储路径</param>
        /// <returns>true-保存成功,false-失败</returns>
        public bool SaveXml(string XPathString, string FilePath)
        {
            if (!CFile.FilePathIsValid(m_FilePath, ".xml"))
                return false;
            XmlNode _XmlNode = FindXmlNode(XPathString);
            if (_XmlNode != null)
            {
                SaveXml(_XmlNode, FilePath);
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 删除一个子结点
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式</param>
        public void RemoveChild(string XPathString)
        {
            XmlNode _XmlNode = FindXmlNode(XPathString);
            if (_XmlNode != null)
            {
                XmlNode _ParentNode = _XmlNode.ParentNode;
                _ParentNode.RemoveChild(_XmlNode);
            }
        }

        /// <summary>
        /// 返回子节点个数(查询到的节点子节点个数)
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <returns>子结点个数</returns>
        public int GetChildCount(string XPathString)
        {
            XmlNode _FindNode = FindXmlNode(XPathString);
            if (_FindNode == null)
                return 0;
            return _FindNode.ChildNodes.Count;
        }

        /// <summary>
        /// 主节点下查询到的节点的节点值
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <returns>节点值</returns>
        public string GetNodeValue(string XPathString)
        {
            XmlNode _XmlNode = FindXmlNode(XPathString);
            if (_XmlNode != null)
                return GetNodeValue(_XmlNode);
            else
                return String.Empty;
        }

        /// <summary>
        /// 移除主节点下查询到的子节点属性
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <param name="DelAttributeName">属性名</param>
        /// <returns>true-成功,false-失败</returns>
        public bool RemoveAttribute(string XPathString, string DelAttributeName)
        {
            XmlNode _XmlNode = FindXmlNode(XPathString);
            if (_XmlNode != null)
                return RemoveAttribute(_XmlNode, DelAttributeName);
            else
                return false;
        }

        /// <summary>
        /// 获取主节点对应子节点属性值
        /// </summary>
        /// <param name="XPathString">XPath查询表达式，为空的时候表示查询主节点</param>
        /// <param name="AttributeName">属性名称</param>
        /// <returns></returns>
        public string GetAttributeValue(string XPathString, string AttributeName)
        {
            if (XPathString.Trim().Length == 0)
                return String.Empty;
            else
            {
                XmlNode _XmlFindNode = FindXmlNode(XPathString);
                if (_XmlFindNode == null)
                    return String.Empty;
                return GetNodeAttributeValue(_XmlFindNode, AttributeName);
            }
        }

        /// <summary>
        /// 修改查询到的节点的对应属性值
        /// </summary>
        /// <param name="xPath">XPath查询表达式,为空的时候表示查询主节点</param>
        /// <param name="attributeValues">格式:属性名称|属性值</param>
        /// <returns>true-修改成功,false-失败(失败就表示不存在该属性或节点)</returns>
        public bool EditAttributeValue(string XPathString, params string[] AttributeValues)
        {
            return EditAttributeValue(XPathString, m_FilePath, AttributeValues);
        }

        /// <summary>
        /// 获取主节点对应子节点多个并列属性值
        /// </summary>
        /// <param name="XPathString">XPath查询表达式,为空的时候表示查询主节点</param>
        /// <param name="AttributeNames">并列的属性名称</param>
        /// <returns>属性值</returns>
        public string[] GetAttributesValue(string XPathString, params string[] AttributeNames)
        {
            XmlNode _XmlFindNode;
            string[] _ReturnValues = new string[AttributeNames.Length];
            if (XPathString.Trim() != String.Empty)
            {
                _XmlFindNode = FindXmlNode(XPathString);
                if (_XmlFindNode == null)
                    return _ReturnValues;
            }
            else
                return new string[] { };
            for (int _i = 0; _i < _ReturnValues.Length; _i++)
            {
                _ReturnValues[_i] = GetNodeAttributeValue(_XmlFindNode, AttributeNames[_i]);
            }
            return _ReturnValues;
        }

        /// <summary>
        /// 修改节点的对应属性值 
        /// </summary>
        /// <param name="XPathString">XPath查询表达式,为空的时候表示当前节点</param>
        /// <param name="FileName">XML文件路径</param>
        /// <param name="Attributes">格式:属性名称|属性值</param>
        /// <returns>true-修改成功,false失败(失败就表示不存在该属性或节点)</returns>
        public bool EditAttributeValue(string XPathString, string FileName, params string[] Attributes)
        {
            XmlDocument _XmlDom = new XmlDocument();
            _XmlDom.Load(FileName);
            XmlNode _XmlFindNode = _XmlDom.SelectSingleNode(XPathString);
            if (_XmlFindNode == null)
                return false;
            XmlAttribute _FindAttribute;
            for (int i = 0; i < Attributes.Length; i++)
            {
                string[] _AttributeValues = Attributes[i].Split('|');
                if (_AttributeValues.Length == 0)
                    return false;
                _FindAttribute = _XmlFindNode.Attributes[_AttributeValues[0]];
                if (_FindAttribute == null)
                    return false;
                _FindAttribute.Value = _AttributeValues[1];
            }
            _XmlDom.Save(FileName);
            return true;
        }

        /// <summary>
        /// 获取当前节点属性值
        /// </summary>
        /// <param name="XmlNowNode">需要获取属性值的节点</param>
        /// <param name="AttributeName">对应节点属性名称</param>
        /// <returns>成功-属性值,失败-空串</returns>
        public static string GetNodeAttributeValue(XmlNode XmlNowNode, string AttributeName)
        {
            XmlAttribute _NowAttribute = XmlNowNode.Attributes[AttributeName];
            if (_NowAttribute == null)
                return String.Empty;
            _NowAttribute = null;
            return XmlNowNode.Attributes[AttributeName].Value;
        }

        /// <summary>
        /// 获取当前节点属性值
        /// </summary>
        /// <param name="XmlNowNode">需要获取属性值的节点</param>
        /// <param name="XPathString">XPath子结点查询表达式</param>
        /// <param name="AttributeName">对应节点属性名称</param>
        /// <returns>成功-属性值,失败-空串</returns>
        public string GetNodeAttributeValue(XmlNode XmlNowNode, string XPathString, string AttributeName)
        {
            XmlNode _XmlFindNode = FindXPath(XmlNowNode, XPathString);
            if (_XmlFindNode == null)
                return String.Empty;
            return GetNodeAttributeValue(_XmlFindNode, AttributeName);
        }

        /// <summary>
        /// 移除当前节点属性
        /// </summary>
        /// <param name="XmlNowNode">需要移除属性的节点</param>
        /// <param name="DelAttributeName">需要移除的属性名称</param>
        /// <returns>true-成功,false-失败</returns>
        public static bool RemoveAttribute(XmlNode XmlNowNode, string DelAttributeName)
        {
            if (XmlNowNode.Attributes[DelAttributeName] == null)
                return false;
            if (XmlNowNode.Attributes.RemoveNamedItem(DelAttributeName) != null)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 移除主节点下查询到的子节点的属性
        /// </summary>
        /// <param name="XmlNowNode">主节点对象</param>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <param name="DelAttributeName">需要移除的属性名称</param>
        /// <returns>true-成功,false-失败</returns>
        public bool RemoveAttribute(XmlNode XmlNowNode, string XPathString, string DelAttributeName)
        {
            XmlNode _XmlFindNode = FindXPath(XmlNowNode, XPathString);
            if (_XmlFindNode == null)
                return false;
            else
                return RemoveAttribute(_XmlFindNode, DelAttributeName);
        }

        /// <summary>
        /// 获取当前节点值
        /// </summary>
        /// <param name="XmlNowNode">当前节点对象</param>
        /// <returns>当前节点值</returns>
        public static string GetNodeValue(XmlNode XmlNowNode)
        {
            if (XmlNowNode == null)
                return String.Empty;
            else
                return XmlNowNode.ChildNodes[0].Value;
        }

        /// <summary>
        /// 获取需要查询的子节点值
        /// </summary>
        /// <param name="ParentNode">父域节点</param>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <returns></returns>
        public string GetNodeValue(XmlNode ParentNode, string XPathString)
        {
            return GetNodeValue(FindXPath(ParentNode, XPathString));
        }

        /// <summary>
        /// 修改指定节点的子节点文本
        /// </summary>
        /// <param name="XPathString">父节点的XPath查询表达式</param>
        /// <param name="InnerTexts"></param>
        /// <returns></returns>
        public bool EditNodeValue(string XPathString, params string[] InnerTexts)
        {
            XmlDocument _XmlDom = new XmlDocument();
            _XmlDom.Load(m_FilePath);
            XmlNode _XmlFindNode = _XmlDom.SelectSingleNode(XPathString);
            if (_XmlFindNode == null)
                return false;
            for (int i = 0; i < _XmlFindNode.ChildNodes.Count; i++)
            {
                if (i < InnerTexts.Length)
                {
                    _XmlFindNode.ChildNodes[i].InnerText = InnerTexts[i];
                }
            }
            _XmlDom.Save(m_FilePath);
            return true;
        }

        /// <summary>
        /// 保存XML文档
        /// </summary>
        /// <param name="saveNode">需要保存的XML节点</param>
        /// <param name="saveFilePath">存储路径</param>
        public void SaveXml(XmlNode saveNode, string saveFilePath)
        {
            FileStream _TmpStream = CFile.Create(saveFilePath);
            if (_TmpStream != null)
            {
                _TmpStream.Close();
            }
            XmlWriter _Writer = XmlWriter.Create(saveFilePath);
            saveNode.WriteTo(_Writer);          //写入文件
            _Writer.Flush();
            _Writer.Close();
        }

        /// <summary>
        /// 保存XML文档
        /// </summary>
        /// <param name="SaveNode"></param>
        /// <param name="AttributeValue"></param>
        /// <param name="NodeText"></param>
        public void SaveXml(XmlNode SaveNode, string AttributeValue, string NodeText)
        {
            //
        }

        /// <summary>
        /// 查询一个节点对象
        /// </summary>
        /// <param name="ParentNode">父域节点对象</param>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <returns>非null-成功, null-失败</returns>
        public XmlNode FindXPath(XmlNode ParentNode, string XPathString)
        {
            try
            {
                if (XPathString.Trim().Length == 0)
                    return ParentNode;
                return ParentNode.SelectSingleNode(XPathString);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 查询一个节点对象
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <returns>true-成功,false-失败</returns>
        public bool FindXPath(string XPathString)
        {
            try
            {
                if (XPathString.Trim().Length == 0)
                    return false;
                else
                {
                    XmlNode _XmlNode = LoadXml();
                    if (_XmlNode != null)
                    {
                        XmlNode _FindNode = FindXPath(_XmlNode, XPathString);
                        if (_FindNode != null && !_FindNode.Equals(_XmlNode))
                            return true;
                        else
                            return false;
                    }
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 查询一个节点对象
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式</param>
        /// <returns>非null-成功, null-失败</returns>
        public XmlNode FindXmlNode(string XPathString)
        {
            try
            {
                if (XPathString.Trim().Length == 0)
                    return null;
                else
                {
                    XmlNode _XmlNode = LoadXml();
                    if (_XmlNode != null)
                    {
                        XmlNode _NewNode = FindXPath(_XmlNode, XPathString);
                        return _NewNode;
                    }
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 移除指定子结点
        /// </summary>
        /// <param name="ParentNode">父域节点</param>
        /// <param name="XPathString">XPath查询节点(欲删除节点)表达式</param>
        /// <returns>删除节点的父节点</returns>
        public XmlNode RemoveChildNode(XmlNode ParentNode, string XPathString)
        {
            if (XPathString.Trim().Length == 0)
                return ParentNode;
            XmlNode _DeleteNode = ParentNode.SelectSingleNode(XPathString);
            if (_DeleteNode == null)
                return ParentNode;
            return RemoveChildNode(ParentNode, _DeleteNode);

        }

        /// <summary>
        /// 移除指定子结点
        /// </summary>
        /// <param name="ParentNode">父域节点</param>
        /// <param name="DeletedNode">指定删除的子结点</param>
        /// <returns>删除节点的父节点</returns>
        public XmlNode RemoveChildNode(XmlNode ParentNode, XmlNode DeletedNode)
        {
            ParentNode.RemoveChild(DeletedNode);
            return ParentNode;
        }

        /// <summary>
        /// 创建一个XML节点
        /// </summary>
        /// <param name="NodeName">节点名称</param>
        /// <param name="AttributeValues">
        /// 数组奇数下标为节点属性,偶数下标为节点属性值,数组长度为单数则具有节点值
        /// </param>
        /// <returns>新XML节点</returns>
        public XmlNode CreateXmlNode(string NodeName, params string[] AttributeValues)
        {
            if (!CFile.FilePathIsValid(m_FilePath, ".xml"))
                return null;
            XmlDocument _XmlDom = new XmlDocument();
            _XmlDom.Load(m_FilePath);
            XmlNode _XmlNewNode = _XmlDom.CreateNode(XmlNodeType.Element, NodeName, "");
            if (AttributeValues.Length == 0)
                return _XmlNewNode;
            if (AttributeValues.Length % 2 == 0)
            {
                for (int Int_i = 0; Int_i < AttributeValues.Length; Int_i += 2)
                {
                    XmlAttribute _XmlAttr = _XmlDom.CreateAttribute(AttributeValues[Int_i]);
                    _XmlAttr.Value = AttributeValues[Int_i + 1];
                    _XmlNewNode.Attributes.SetNamedItem(_XmlAttr);
                }
            }
            else
            {
                for (int Int_i = 0; Int_i < AttributeValues.Length - 1; Int_i += 2)
                {
                    XmlAttribute _XmlAttr = _XmlDom.CreateAttribute(AttributeValues[Int_i]);
                    _XmlAttr.Value = AttributeValues[Int_i + 1];
                    _XmlNewNode.Attributes.SetNamedItem(_XmlAttr);
                }
                _XmlNewNode.AppendChild(_XmlDom.CreateCDataSection(AttributeValues[AttributeValues.Length - 1]));
            }
            return _XmlNewNode;
        }

        /// <summary>
        /// 加载XML文档
        /// </summary>
        /// <param name="FilePath">XML文档路径</param>
        /// <returns>成功-返回根节点,失败-返回null</returns>
        public XmlNode LoadXml(string FilePath)
        {
            if (!CFile.FilePathIsValid(m_FilePath, ".xml"))
                return null;
            try
            {
                if (!System.IO.File.Exists(FilePath))
                {
                    return null;
                }
                XmlDocument _XmlDom = new XmlDocument();
                _XmlDom.Load(FilePath);
                return _XmlDom.DocumentElement.FirstChild;
            }
            catch (DirectoryNotFoundException e)
            {
                return null;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        /// <summary>
        /// 加载XML文档
        /// </summary>
        /// <returns>成功-返回根节点,失败-返回null</returns>
        public XmlNode LoadXml()
        {
            return LoadXml(m_FilePath);
        }

        /// <summary>
        /// 创建xml文件(*.xml)
        /// </summary>
        /// <param name="RootName">根节点名称("COM")</param>
        /// <returns>【true-成功,false-失败】</returns>
        public bool CreateXMLDocument(string RootName)
        {
            if (!CFile.FilePathIsValid(m_FilePath, ".xml"))
                return false;
            try
            {
                // 判断文件是否存在
                if (!System.IO.File.Exists(m_FilePath))
                {
                    XmlTextWriter xmlWriter = new XmlTextWriter(m_FilePath, Encoding.UTF8);
                    xmlWriter.WriteStartDocument();
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.WriteStartElement(RootName);
                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndDocument();
                    xmlWriter.Close();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 创建xml文件(*.xml)
        /// </summary>
        /// <param name="RootName">根节点名称("COM")</param>
        /// <param name="EncodeType">文件编码</param>
        /// <returns>【true-成功,false-失败】</returns>
        public bool CreateXMLDocument(string RootName, EmEncode EncodeType)
        {
            if (!CFile.FilePathIsValid(m_FilePath, ".xml"))
                return false;
            try
            {
                // 判断文件是否存在
                if (!System.IO.File.Exists(m_FilePath))
                {
                    XmlTextWriter xmlWriter;
                    if (EncodeType == EmEncode.Unicode)
                        xmlWriter = new XmlTextWriter(m_FilePath, Encoding.Unicode);
                    else if (EncodeType == EmEncode.ASCII)
                        xmlWriter = new XmlTextWriter(m_FilePath, Encoding.ASCII);
                    else if (EncodeType == EmEncode.GB2312)
                        xmlWriter = new XmlTextWriter(m_FilePath, Encoding.GetEncoding("GB2312"));
                    else
                        xmlWriter = new XmlTextWriter(m_FilePath, Encoding.UTF8);
                    xmlWriter.WriteStartDocument();
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.WriteStartElement(RootName);
                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndDocument();
                    xmlWriter.Close();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 读取XML文档指定节点的数据
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式(指定的节点的父节点)</param>
        /// <param name="LstNodeText">子节点文本信息</param>
        public void GetNodeText(string XPathString, ref Dictionary<string, string> LstNodeText)
        {
            if (!CFile.FilePathIsValid(m_FilePath, ".xml"))
                return;
            if (LstNodeText == null)
                LstNodeText = new Dictionary<string, string>();
            LstNodeText.Clear();
            XmlNode _XmlNode = LoadXml(m_FilePath);
            if (_XmlNode != null)
            {
                XmlNode _FatherNode = FindXmlNode(XPathString);
                if (_FatherNode != null)
                    for (int i = 0; i < _FatherNode.ChildNodes.Count; i++)
                        LstNodeText.Add(_FatherNode.ChildNodes[i].Name, _FatherNode.ChildNodes[i].InnerText);
            }
        }

        /// <summary>
        /// 读取XML文档指定节点的数据
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式(指定的节点的父节点)</param>
        /// <param name="ChildNodeName">子节点名称</param>
        /// <returns>成功-返回子节点值,失败-空串</returns>
        public string GetNodeText(string XPathString, string ChildNodeName)
        {
            if (!CFile.FilePathIsValid(m_FilePath, ".xml"))
                return String.Empty;
            XmlNode _FatherNode = FindXmlNode(XPathString);
            if (_FatherNode != null)
                for (int i = 0; i < _FatherNode.ChildNodes.Count; i++)
                    if (_FatherNode.ChildNodes[i].Name == ChildNodeName)
                        return _FatherNode.ChildNodes[i].InnerText;
            return string.Empty;
        }

        /// <summary>
        /// 读取XML文档指定节点的数据
        /// </summary>
        /// <param name="XPathString">XPath查询节点表达式(指定的节点)</param>
        /// <returns>成功-返回子节点值,失败-空串</returns>
        public string GetNodeText(string XPathString)
        {
            if (!CFile.FilePathIsValid(m_FilePath, ".xml"))
                return String.Empty;
            XmlNode _XmlNode = FindXmlNode(XPathString);
            if (_XmlNode != null)
                return _XmlNode.InnerText;
            else
                return string.Empty;
        }

        /// <summary>
        /// 返回XPath节点查询表达式
        /// </summary>
        /// <param name="nodePath">
        /// 格式为1,1-1,1-2,...,1-n|2,2-1,...,2-n
        /// {1,2为节点名称,1-1(节点属性名),1-2(节点属性值),以此类推为节点属性名,节点属性值}
        /// </param>
        /// <returns>XPath查询表达式</returns>
        public static string GetXPath(string nodePath)
        {
            string _XPath = String.Empty;
            string[] _TmpXPath = nodePath.Split('|');   //分割获取需要查询嵌套层次的子节点
            for (int i = 0; i < _TmpXPath.Length; i++)
            {
                string[] _TmpNodeParams = _TmpXPath[i].Split(',');  //分割一个节点的表达式内容
                if (_TmpNodeParams.Length % 2 == 0)   //如果长度是2的整数倍，则表达式有错误
                    return string.Empty;              //直接返回空
                if (_XPath == String.Empty)
                    _XPath += string.Format("/{0}", _TmpNodeParams[0]);   //组合表达式
                else
                    _XPath += string.Format("/{0}", _TmpNodeParams[0]);  //组合表达式
                for (int j = 1; j < _TmpNodeParams.Length; j += 2)    //组合表达式属性
                    _XPath += string.Format("[@{0}='{1}']", _TmpNodeParams[j], _TmpNodeParams[j + 1]);
            }
            return _XPath;
        }

        /// <summary>
        /// 获取节点文本
        /// </summary>
        /// <param name="ParentNode"></param>
        /// <param name="LstNodeText"></param>
        public void GetNodeText(XmlNode ParentNode, ref Dictionary<string, string> LstNodeText)
        {
            if (LstNodeText == null)
                LstNodeText = new Dictionary<string, string>();
            if (ParentNode != null)
                for (int i = 0; i < ParentNode.ChildNodes.Count; i++)
                    LstNodeText.Add(ParentNode.ChildNodes[i].Name, ParentNode.ChildNodes[i].InnerText);
        }
        #endregion
    }
}
